<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Logic;


use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class LuckyDrawLogic extends Logic
{
    public function listLogic($uid)
    {
        $lottery_count =  Redis::hGet('LUCKY_DRAW_COUNT',$uid);
        return [
            'uid' => $uid,
            'lottery_count' => is_null($lottery_count) ? 3 : 3 - $lottery_count,
            'item_rand' => 16,
            'item_rotate' => [
                [
                    'id' => 10017,
                    'rotate' => 360,
                ],
                [
                    'id' => 10018,
                    'rotate' => 36,
                ],
                [
                    'id' => 10019,
                    'rotate' => 72,
                ],
                [
                    'id' => 10020,
                    'rotate' => 108,
                ],
                [
                    'id' => 10021,
                    'rotate' => 144,
                ],
                [
                    'id' => 10022,
                    'rotate' => 180,
                ],
                [
                    'id' => 10023,
                    'rotate' => 216,
                ],
                [
                    'id' => 10024,
                    'rotate' => 252,
                ],
                [
                    'id' => 10025,
                    'rotate' => 288,
                ],
                [
                    'id' => 10026,
                    'rotate' => 324,
                ],
            ]
        ];
    }

    public function handleLogic($uid)
    {
        $lucky_draw_count =  Redis::hGet('LUCKY_DRAW_COUNT',$uid);
        if (!is_null($lucky_draw_count) && $lucky_draw_count >= 3) {
            throw new ApiException('今天免费抽奖次数剩余0次',6);
        }
        $prize = [
            ['id' => 10017,'desc' => '888金币卡','value' => 5],
            ['id' => 10018,'desc' => '1888金币卡','value' => 5],
            ['id' => 10019,'desc' => '2888金币卡','value' => 5],
            ['id' => 10020,'desc' => '3888金币卡','value' => 15],
            ['id' => 10021,'desc' => '4888金币卡','value' => 20],
            ['id' => 10022,'desc' => '5888金币卡','value' => 20],
            ['id' => 10023,'desc' => '6888金币卡','value' => 15],
            ['id' => 10024,'desc' => '8888金币卡','value' => 5],
            ['id' => 10025,'desc' => '12888金币卡','value' => 5],
            ['id' => 10026,'desc' => '18888金币卡','value' => 5],
        ];
        $sum = array_sum(array_column($prize,'value'));
        $response = [];
        foreach ($prize as $value) {
            $rand = mt_rand(1,$sum);
            if ($rand <= $value['value']) {
                $response['uid'] = $uid;
                $response['id'] = $value['id'];
                $response['desc'] = $value['desc'];
                break;
            } else {
                $sum -= $value['value'];
            }
        }

        if ($this->addUserItemRec($response['id'],$uid)) {
            Redis::hSet('LUCKY_DRAW_COUNT',$uid,(int)$lucky_draw_count + 1);
            Redis::expire('LUCKY_DRAW_COUNT',strtotime(date('Y-m-d',strtotime('+1 day'))));
            return $response;
        } else {
            throw new ApiException('抽奖失败',6);
        }
    }


    private function addUserItemRec($id,$uid)
    {
        if (DB::table('t_user_bag_info')->where('item_id',$id)->where('uid',$uid)->value('item_id')) {
            if (!DB::table('t_user_bag_info')->where('item_id',$id)->where('uid',$uid)->update(['num' => DB::raw("num + 1")])) {
                return false;
            }
        } else {
            if (!DB::table('t_user_bag_info')->insert([
                'uid' => $uid,
                'item_id' => $id,
                'num' => 1,
            ])
            ) {
                return false;
            }
        }
        return true;
    }
}